package com.sinothk.fc.storeOrder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.goods.entity.GoodsDO;
import com.sinothk.fc.goodsStock.entity.GoodsStockEntity;
import com.sinothk.fc.storeOrder.entity.OrderAddReq;
import com.sinothk.fc.storeOrder.entity.StoreOrderEntity;
import com.sinothk.fc.storeOrder.entity.StoreOrderSearchReq;
import com.sinothk.fc.storeOrder.service.IStoreOrderService;
import com.sinothk.fc.storeOrderDetail.entity.StoreOrderDetailEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 门店订单表
 * @Author: Liangyt
 * @Date: 2022-09-14
 * @Version: V1.0
 */
@Api(tags = "门店订单表")
@RestController
@RequestMapping("/fc/storeOrderCtrl")
@Slf4j
public class StoreOrderController extends JeecgController<StoreOrderEntity, IStoreOrderService> {

    private final IStoreOrderService storeOrderService;

    public StoreOrderController(IStoreOrderService storeOrderService) {
        this.storeOrderService = storeOrderService;
    }

    /**
     * 分页列表查询
     *
     * @param storeOrderEntity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "门店订单表-分页列表查询")
    @ApiOperation(value = "门店订单表-分页列表查询", notes = "门店订单表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StoreOrderEntity storeOrderEntity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<StoreOrderEntity> queryWrapper = QueryGenerator.initQueryWrapper(storeOrderEntity, req.getParameterMap());
        Page<StoreOrderEntity> page = new Page<StoreOrderEntity>(pageNo, pageSize);
        IPage<StoreOrderEntity> pageList = storeOrderService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param storeOrderEntity
     * @return
     */
    @AutoLog(value = "门店订单表-添加")
    @ApiOperation(value = "门店订单表-添加", notes = "门店订单表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StoreOrderEntity storeOrderEntity) {
        storeOrderService.save(storeOrderEntity);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param storeOrderEntity
     * @return
     */
    @AutoLog(value = "门店订单表-编辑")
    @ApiOperation(value = "门店订单表-编辑", notes = "门店订单表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StoreOrderEntity storeOrderEntity) {
        storeOrderService.updateById(storeOrderEntity);
        return Result.OK("编辑成功!");
    }

//    /**
//     * 通过id删除
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "门店订单表-通过id删除")
//    @ApiOperation(value = "门店订单表-通过id删除", notes = "门店订单表-通过id删除")
//    @DeleteMapping(value = "/delete")
//    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
//        storeOrderService.removeById(id);
//        return Result.OK("删除成功!");
//    }
//
//    /**
//     * 批量删除
//     *
//     * @param ids
//     * @return
//     */
//    @AutoLog(value = "门店订单表-批量删除")
//    @ApiOperation(value = "门店订单表-批量删除", notes = "门店订单表-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
//    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        this.storeOrderService.removeByIds(Arrays.asList(ids.split(",")));
//        return Result.OK("批量删除成功!");
//    }
//
//    /**
//     * 通过id查询
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "门店订单表-通过id查询")
//    @ApiOperation(value = "门店订单表-通过id查询", notes = "门店订单表-通过id查询")
//    @GetMapping(value = "/queryById")
//    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
//        StoreOrderEntity storeOrderEntity = storeOrderService.getById(id);
//        if (storeOrderEntity == null) {
//            return Result.error("未找到对应数据");
//        }
//        return Result.OK(storeOrderEntity);
//    }

//    /**
//     * 导出excel
//     *
//     * @param request
//     * @param storeOrderEntity
//     */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, StoreOrderEntity storeOrderEntity) {
//        return super.exportXls(request, storeOrderEntity, StoreOrderEntity.class, "门店订单表");
//    }
//
//    /**
//     * 通过excel导入数据
//     *
//     * @param request
//     * @param response
//     * @return
//     */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, StoreOrderEntity.class);
//    }

    /**
     * 订单-销售添加
     *
     * @param payInfoForSaleReq
     * @return
     */
    @AutoLog(value = "门店订单表-销售添加")
    @ApiOperation(value = "门店订单表-销售添加", notes = "门店订单表-销售添加")
    @PostMapping(value = "/storeOrderAdd")
    public Result<?> storeOrderAdd(@RequestBody OrderAddReq<StoreOrderDetailEntity> payInfoForSaleReq) throws Exception {
        storeOrderService.addPayInfoForSale(payInfoForSaleReq);
        return Result.OK("添加成功！");
    }

    /**
     * 订单分页-门店
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "订单分页-门店")
    @ApiOperation(value = "订单分页-门店", notes = "订单分页-门店")
    @GetMapping(value = "/listForStore")
    public Result<?> listForStore(
            StoreOrderSearchReq searchReq,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<StoreOrderEntity> pageList = storeOrderService.listForStore(new Page<>(pageNo, pageSize), searchReq);
        return Result.OK(pageList);
    }

    /**
     * 订单-销售退货
     *
     * @param storeRecordId
     * @return
     */
    @AutoLog(value = "门店订单表-销售退货")
    @ApiOperation(value = "门店订单表-销售退货", notes = "门店订单表-销售退货")
    @DeleteMapping(value = "/storeOrderRemove")
    public Result<?> storeOrderRemove(@RequestParam(name = "storeRecordId") String storeRecordId) throws Exception {
        storeOrderService.storeOrderRemove(storeRecordId);
        return Result.OK("添加成功！");
    }

    /**
     * 订单-进货添加
     *
     * @param beanReq
     * @return
     */
    @AutoLog(value = "门店订单表-进货添加")
    @ApiOperation(value = "门店订单表-进货添加", notes = "门店订单表-进货添加")
    @PostMapping(value = "/storeOrderForBuy")
    public Result<?> storeBuyOrder(@RequestBody OrderAddReq<GoodsStockEntity> beanReq) throws Exception {
        storeOrderService.storeOrderForBuy(beanReq);
        return Result.OK("添加成功！");
    }

    /**
     * 订单-销售批量添加
     *
     * @param addReq
     * @return
     */
    @AutoLog(value = "订单-销售批量添加")
    @ApiOperation(value = "订单-销售批量添加", notes = "订单-销售批量添加")
    @PostMapping(value = "/addSaleOrder")
    public Result<?> addSaleOrder(@RequestBody OrderAddReq<GoodsDO> addReq) throws Exception {
        storeOrderService.addSaleOrder(addReq);
        return Result.OK("添加成功！");
    }

//    /**
//     * 订单分页-按订单id分组查询
//     *
//     * @param pageNo
//     * @param pageSize
//     * @return
//     */
//    @Deprecated
//    @AutoLog(value = "订单分页-按订单id分组查询")
//    @ApiOperation(value = "订单分页-按订单id分组查询", notes = "订单分页-按订单id分组查询")
//    @GetMapping(value = "/pageForStoreGroupByOrderId")
//    public Result<?> pageForStoreGroupByOrderId(
//            StoreOrderSearchReq searchReq,
//            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
//            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
//        IPage<StoreOrderEntity> pageList = storeOrderService.pageForStoreGroupByOrderId(new Page<>(pageNo, pageSize), searchReq);
//        return Result.OK(pageList);
//    }
}
